package com.key.controller.user;

import com.key.common.R;
import com.key.common.dto.*;
import com.key.common.vo.AssetsVO;
import com.key.common.vo.CanReturnVO;
import com.key.common.vo.CanWithdrawalVO;
import com.key.entity.Assets;
import com.key.entity.BorrowAssets;
import com.key.entity.ReceiptAssets;
import com.key.entity.ReturnAssets;
import com.key.service.*;
import com.key.service.impl.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author tc
 * @create 2023-02-17 10:24
 */
@Api(value = "用户申请",tags = "用户申请接口API")
@Controller
@RequestMapping(value = "/usrApply" )
public class UsrApplyController {

    @Autowired
    BorrowAssetsService borrowAssetsService;
    @Autowired
    ReturnAssetsService returnAssetsService;
    @Autowired
    ReceiptAssetsService receiptAssetsService;
    @Autowired
    WithdrawalAssetsService withdrawalAssetsService;
    @Autowired
    AssetTypeService assetTypeService;
    @Autowired
    AssetsService assetsService;
    @Autowired
    ApplyService applyService;

    //已完成
    @ResponseBody
    @RequestMapping(value = "/showAllApply",method = RequestMethod.POST)
    @ApiOperation(value = "显示所有申请单",notes = "页面显示申请单",httpMethod = "post")
    public R showAllApply(@RequestBody @ApiParam(name = "applyDTO",value = "搜说条件(时间单位为月1，3，6)",required = true) ApplyDTO applyDTO){
        return applyService.findAllApply(applyDTO);
    };

    //已完成
    /**
     * 领用资产申请
     * @author tc
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRequisitionAssets",method = RequestMethod.POST)
    @ApiOperation(value = "用户领用申请接口",notes = "对用户领用资产进行操作",httpMethod = "post")
    public R addRequisitionAssets(@RequestBody @ApiParam(name = "receiptAssetsDTO",value = "资产领用时需要填入的属性",required = true) ReceiptAssetsDTO receiptAssetsDTO){
        List<Long> assetsId=receiptAssetsDTO.getAssetsId();//获取json中选中的资产编号
        ReceiptAssets receiptAssets =receiptAssetsDTO.getReceiptAssets();
        R r=receiptAssetsService.creatReceipt(assetsId,receiptAssets);
        return r;
    };

    //已完成
    /**
     * 借用资产申请
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/addBorrowedAssets",method = RequestMethod.POST)
    @ApiOperation(value = "用户借用申请接口",notes = "对用户借用资产进行操作",httpMethod = "post")
    public R addBorrowedAssets(@RequestBody  @ApiParam(name = "borrowAssetsDTO",value = "资产借用时需要填入的属性",required = true) BorrowAssetsDTO borrowAssetsDTO){
        List<Long> assetsId=borrowAssetsDTO.getAssetsId();//获取json中选中的资产编号
        BorrowAssets borrowAssets=borrowAssetsDTO.getBorrowAssets();
        R r=borrowAssetsService.creatBorrowed(assetsId,borrowAssets);
        return r;
    }

    //已完成
    /**
     * 领用，借用资产时的资产类别显示
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showAssetClass",method = RequestMethod.GET)
    @ApiOperation(value = "领用，申请资产时的资产类别接口",notes = "对用户领用，申请资产时资产类的显示",httpMethod = "get")
    public R showAssetClass(){
        return  assetTypeService.findAllAssetsType();
    };


    //已完成
    /**
     * 领用，借用请资产时显示可用的资产
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showAssetsByAssetClass",method = RequestMethod.GET)
    @ApiOperation(value = "领用，申请资产时可用接口",notes = "领用，申请资产时依据资产类别显示资产",httpMethod = "get")
    public R showAssetsByAssetClass(@ApiParam(name = "assetTypeName",value = "资产的类别(String)",required = true)String assetTypeName ){
        return assetsService.findAssetsListByAssetClassAndAssetsState(assetTypeName);
    };


    //已完成
    /**
     * 退库资产时显示可退库资产
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showCanWithdrawal",method = RequestMethod.GET)
    @ApiOperation(value = "退库时显示可退库资源接口",notes = "退库时显示可退库资源",httpMethod = "get")
    public R showCanWithdrawal(){
        R r=new R();
        List<CanWithdrawalVO> canWithdrawalVOList=new ArrayList<>();
        List<ReceiptAssets> receiptAssetsList=receiptAssetsService.findCanWithdrawalReceiptAssets();
        if(receiptAssetsList!=null){
            for (ReceiptAssets R:receiptAssetsList) {
                CanWithdrawalVO canWithdrawalVO=new CanWithdrawalVO();
                List<Assets> assetsList= receiptAssetsService.findAssetsByReceiptAssets(R);
                if(assetsList!=null){
                    canWithdrawalVO.setReceiptAssets(R);
                    canWithdrawalVO.setAssetsList(assetsList);
                    canWithdrawalVOList.add(canWithdrawalVO);
                }
            }
            r.setCode(200);
            r.setData(canWithdrawalVOList);
        }else {
            r.setCode(500);
            r.setMsg("错误");
        }
        return r;
    };


    //已完成
    /**
     * 归还资产时显示可归还资产(依据单号分类)
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/showCanReturn",method = RequestMethod.GET)
    @ApiOperation(value = "归还时显示可退库资源接口",notes = "归还时显示可退库资源",httpMethod = "get")
    public R showCanReturn(){
        R r=new R();
        List<CanReturnVO> canReturnVOList=new ArrayList<>();
        List<BorrowAssets> borrowAssetsList=borrowAssetsService.findCanReturnBorrowAssets();
        if(borrowAssetsList!=null){
            for (BorrowAssets B:borrowAssetsList) {
                CanReturnVO canReturnVO=new CanReturnVO();
                List<Assets> assetsList= borrowAssetsService.findAssetsByBorrowAssets(B);
                if(assetsList!=null){
                    canReturnVO.setBorrowAssets(B);
                    canReturnVO.setAssetsList(assetsList);
                    canReturnVOList.add(canReturnVO);
                }
            }
            r.setCode(200);
            r.setData(canReturnVOList);
        }else {
            r.setCode(500);
            r.setMsg("错误");
        }
        return r;
    };

    //未测试
    /**
     * 退库资产申请
     * @author tc
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addWithdrawalAssets",method = RequestMethod.POST)
    @ApiOperation(value = "用户退库申请接口",notes = "对用户退库资产进行操作",httpMethod = "post")
    public R addWithdrawalAssets(@RequestBody @ApiParam(name = "withdrawalAssetsDTO",value = "选中单号的资源",required = true) WithdrawalAssetsDTO withdrawalAssetsDTO){

        return withdrawalAssetsService.creatWithdrawal(withdrawalAssetsDTO);
    };

    //未测试
    /**
     * 归还资产申请
     * @author tc
     */
    @ResponseBody
    @RequestMapping(value = "/addReturnAssets",method = RequestMethod.POST)
    @ApiOperation(value = "用户归还申请接口",notes = "对用户归还资产进行操作",httpMethod = "post")
    public R addReturnAssets(@RequestBody @ApiParam(name = "borrowAssets",value = "选中单号的资源",required = true) ReturnAssetsDTO returnAssetsDTO){
        return returnAssetsService.creatReturn(returnAssetsDTO);
    };



}
